package com.jvtd.eyl.ui.login;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jvtd.eyl.R;
import com.jvtd.eyl.base.BaseMvpFragment;
import com.jvtd.eyl.bean.db.User;
import com.jvtd.eyl.databinding.FragmentLoginBinding;
import com.jvtd.eyl.ui.main.MainActivity;
import com.trusfort.sdk.callback.TFCallback;
import com.trusfort.sdk.iml.TrusfortApi;

import javax.inject.Inject;

/**
 * 登录
 * <p>
 * 作者:chenlei
 * 时间:2018/11/12 3:17 PM
 */
public class LoginFragment extends BaseMvpFragment implements LoginMvpView, TFCallback {

    @Inject
    LoginPresenter<LoginMvpView> mPresenter;
    private FragmentLoginBinding mBinding;

    public static LoginFragment newInstance() {
        Bundle args = new Bundle();
        LoginFragment fragment = new LoginFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    protected View getLayout(LayoutInflater inflater, @Nullable ViewGroup container) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_login, container,
                false);
        return mBinding.getRoot();
    }

    @Override
    protected void initViewAndData() {
        mPresenter.onAttach(this);
        checkStoragePermission();
    }

    @Override
    public void onDestroyView() {
        mPresenter.onDetach();
        super.onDestroyView();
    }

    @Override
    protected void onGranted() {
        super.onGranted();
//        login();
        testLogin();
    }

    /**
     * 测试登录
     *
     * @author Chenlei
     * created at 2018/11/12
     **/
    private void testLogin() {
//        mPresenter.loginAccount("5665f76a496249fe8be77088df6a092e");
//        mPresenter.loginAccount("4eb34b673a9e4781ab6f8b8de0409ba3");
        User user = new User();
        user.setUserId("7698");
        user.setDepartment("ou=网信e评估测试,ou=蓝信测试服务（10人）");
        user.setName("李丽雨");
        user.saveOrUpdate();
        loginSuccess();
    }

    /**
     * 网信办用户信息获取
     *
     * @author Chenlei
     * created at 2018/11/12
     **/
    private void login() {
        TrusfortApi.getInstence().getTicket(mContext, this);
    }

    /**
     * 网信办登录成功
     *
     * @param s token
     */
    @Override
    public void onSuccess(String s) {
        mPresenter.loginAccount(s);
    }

    /**
     * 网信办登录失败
     *
     * @param i code
     * @param s 错误信息
     */
    @Override
    public void onFail(int i, String s) {
        showMessage(s);
//        loginFailed();
    }

    @Override
    public void loginSuccess() {
        //跳转首页
        startActivity(MainActivity.getIntent(mContext));
        //销毁当前界面
        if (getActivity() != null)
            getActivity().finish();
    }

    @Override
    public void loginFailed(String msg) {
        if (TextUtils.isEmpty(msg)){
            showMessage(R.string.login_failed);
        }else {
            showMessage(msg);
        }
    }
}
